const router = require('koa-router')();
const users = require('../data/user');
const routers = require('../data/router');

/**
 * 获取用户路由权限列表
 */
router.post('/user_router_auth', async (ctx, next) => {
  //获取前端请求的uid
  const {
    uid
  } = ctx.request.body;

  if (uid) {
    // 装载选好路由的容器
    let authRouterInfo = [];

    //过滤符合id的那一项
    const userInfo = users.filter(user => user.id == uid)[0];
    // console.log(userInfo);

    /**
     * console.log(userInfo);
     * {
     *   id: 1,
     *   name: 'zhangsan',
     *   auth: [2, 3, 6, 7]
     * }
     */

    //遍历用户表
    userInfo.auth.map((rid) => {
      //遍历路由表对比
      routers.map((router) => {
        if (router.id === rid) {
          //将符合的项存入容器
          authRouterInfo.push(router);
        }
      });
    });


    ctx.body = authRouterInfo;
  } else {
    next();
  }

});


module.exports = router